<template>
  <div>
    <div class="t_body_bg" :style="{backgroundImage: 'url('+bgimg+')'}">
      <div style="width: 6.3rem; margin: 0 auto;">

        <div class="t_logo_font flexcc" >
          <span v-if="!logoimg" style="font-size: 0.3rem; font-weight: bold;">logo</span>
          <img :src="logoimg" style="width: 2.5rem;">
        </div>
        <div style="text-align: center; font-size: 0.3rem; font-weight: bold; margin: 0.5rem 0;">{{ comname }}</div>

        <div style="margin-top: 1rem;">
          <div v-if="weixinhuanjing" @click="go2()" style="border-radius: 0.3rem; margin-top: 0.4rem; background: rgb(59, 195, 23); color: rgb(255, 255, 255); font-size: 0.3rem; padding: 0.2rem 0;" class="flexcc">微信登录</div>
          <div @click="go('/mlogin/denglu')" style="border-radius: 0.3rem; margin-top: 0.4rem; background: rgb(255, 255, 255); color: rgb(60, 133, 242); font-size: 0.3rem; padding: 0.2rem 0;" class="flexcc">使用手机号密码登录</div>
        </div>

        <div class="flexc" style="margin-top: 0.5rem; color: rgb(0, 90, 255); font-size: 0.25rem; justify-content: flex-end;" v-if="check_code">

          <div @click="go('/mlogin/zhuce')"><span style="color: rgb(117, 117, 117);">没有账号？</span>去注册</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

	export default {
		name: 'login',
    components:{

    },
		data() {
			return {
				checked: true,//是否记住账户
				logintype:1,
        loginm:1,
				codemsg:'获取验证码',
				form:{
					username:'',//账号名称
					password:'',//密码
					phonenum:'',//手机号
					code:'',//验证码
					yqcode:'',//邀请码
					passwordagin:'',//再次输入密码
				},
				bgimg:require('@/../static/mimg/mbj.jpg'),
				// bgimg:'https://img.jbzj.com/file_images/article/202205/2022053011053840.png',
				logoimg:'',
				comname:'欢迎使用',
				readonly:true,
        		xinxi:"",
				check_code:'',
        shoujiban:false,
        distributor:0,
        icp:"",

        weixinhuanjing:false,
        appid:""

			}
		},
		created() {

      

      var dz_url = window.location.href;//获取地址
      var check_code2 = dz_url.split('?')[1];//获取?之后的参数字符串

      if(check_code2){
        var check_code = decodeURIComponent(check_code2.split('&')[0])
        this.check_code = check_code
      }

      var ua = window.navigator.userAgent.toLowerCase();
      if (ua.match(/micromessenger/i) == 'micromessenger' ) {

      }else{
        this.$router.push('/mlogin/denglu?'+encodeURIComponent(this.check_code))
      }

		  axios.get('/api/logo').then((response)=>{
			if (response.data.msg.code == 0) {

        //微信环境 有appid也就是配置过微信
        axios.get('/api/wechat/get').then((response2)=>{
          if (response2.data.msg.code == 0) {
            this.appid  =  response2.data.data
            if (ua.match(/micromessenger/i) == 'micromessenger' && this.appid) {
                 this.weixinhuanjing = true
            }
          }
        })

			//   this.bgimg = response.data.data.h5_login_background
				if(response.data.data.h5_login_background&&response.data.data.h5_login_background!==''){
				this.bgimg = response.data.data.h5_login_background
			}else{
				this.bgimg = require('@/../static/mimg/mbj.jpg')
			}
			  this.comname = response.data.data.shop_name
			  this.logoimg = response.data.data.system_logo
        this.icp = response.data.data.icp

			  document.title = response.data.data.shop_name
			  localStorage.setItem('shop_name',response.data.data.shop_name)
			  localStorage.setItem('system_logo',response.data.data.system_logo)
			  localStorage.setItem('left_logo',response.data.data.left_logo)
			  localStorage.setItem('icp',response.data.data.icp)



			} else {
				this.$message.error(response.data.msg.info);
			}


		  })


		},
		mounted() {
      if(window.screen.width < 1080){
        this.shoujiban = true
        var h = document.documentElement.clientWidth / 7.5 + 'px';
        document.documentElement.style.fontSize = h;
        // this.getCookie()
      }else{

      }
		},
		methods: {
      go:function(url){
        this.$router.push(url +'?' + encodeURIComponent(this.check_code))
      },
      go2:function(url){
        //有邀请码挑注册  没有邀请码跳登录
        if(this.check_code){
          localStorage.setItem('check_code',this.check_code)
          this.$router.push('/mlogin/bangdingshoujihao')
        }else{
          localStorage.setItem('check_code','')
          this.$router.push('/mlogin/bangdingshoujihaologin')
        }
      },
      yanzhengmadenglu:function(){
        let obj = {
          mobile:this.all3.mobile,
          code:this.all3.code,
        }
        axios.post(`/api/wechat/login2`, obj)
        	.then(response=> {
        		if (response.data.msg.code == 0) {
              //角色是供应商  临时写在这里
              if(response.data.data.is_owner==1){
                axios.get('/own/auth/user').then((response2)=>{
                  if (response2.data.msg.code == 0) {
                    localStorage.setItem('user',JSON.stringify(response2.data.data))
                    this.$nextTick(()=>{
                      if(this.shoujiban){
                        window.open(window.location.origin+'/hou/shangpinguanli/gongyingshangshangpin', "_self")
                      }else{
                        window.open(window.location.origin+'/hou', "_self")
                      }
                    })
                  } else {
                    this.$message.error(response2.data.msg.info);
                  }
                })
              }
              //分销商
              if(response.data.data.is_owner==0){
                axios.get('/agent/auth/user').then((response2)=>{
                  if (response2.data.msg.code == 0) {
                    localStorage.setItem('user',JSON.stringify(response2.data.data))
                    this.$nextTick(()=>{
                      if(this.shoujiban){
                        window.open(window.location.origin+'/web/shangpinguanli/zaishoushangpin', "_self")
                      }else{
                        window.open(window.location.origin+'/web', "_self")
                      }
                    })
                  } else {
                    this.$message.error(response2.data.msg.info);
                  }
                })
              }
        		}else{
              this.$message.error(response.data.msg.info);
            }
        	})
      },


			choosetype(i){
				this.logintype = i
				//重置数据
				this.codemsg = '获取验证码'
				this.form.username = ''
				this.form.password = ''
				this.form.phonenum = ''
				this.form.code = ''
				this.form.yqcode = ''
				this.form.passwordagin = ''
			},
			//注册，登录，找回
			loginorreg(){
				//1登录，2注册，3找回

				if(this.logintype==1){

          if(this.loginm == 1){
            //账号密码
            if(this.form.username==''){
            	this.$message.error('请输入手机号');
            	return
            }
            if(this.form.password==''){
            	this.$message.error('请输入密码');
            	return
            }
            var obg={
            	name:this.form.username,
            	password:this.form.password,
            }
          }

          if(this.loginm == 2){
            //手机号验证码
            if(this.form.phonenum==''){
            	this.$message.error('请输入手机号');
            	return
            }
            if(this.form.code==''){
            	this.$message.error('请输入验证码');
            	return
            }
            var obg={
            	mobile:this.form.mobile,
            	code:this.form.code,
            }
          }


					axios.post('/api/login',obg).then((response)=>{
						if (response.data.msg.code == 0) {
							this.$message({
								message: '登录成功',
								type: 'success'
							});
                //1 管理员 0 用户
                if(response.data.data.is_owner==1){
                  //临时写在这里
                  axios.get('/own/auth/user').then((response2)=>{
                    if (response2.data.msg.code == 0) {
                      localStorage.setItem('user',JSON.stringify(response2.data.data))
                      this.$nextTick(()=>{
                        if(this.shoujiban){
                          window.open(window.location.origin+'/hou/shangpinguanli/gongyingshangshangpin', "_self")
                        }else{
                          window.open(window.location.origin+'/hou', "_self")
                        }
                      })
                    } else {
                      this.$message.error(response2.data.msg.info);
                    }
                  })
                }

                if(response.data.data.is_owner==0){
                  axios.get('/agent/auth/user').then((response2)=>{
                    if (response2.data.msg.code == 0) {
                      localStorage.setItem('user',JSON.stringify(response2.data.data))
                      this.$nextTick(()=>{
                        if(this.shoujiban){
                          window.open(window.location.origin+'/web/shangpinguanli/zaishoushangpin', "_self")
                        }else{
                          window.open(window.location.origin+'/web', "_self")
                        }
                      })
                    } else {
                      this.$message.error(response2.data.msg.info);
                    }
                  })
                }

						} else {
							this.$message.error(response.data.msg.info);
						}
					})


				}
				if(this.logintype==2){
          this.form.username = this.form.phonenum
					//注册
					if(this.form.username==''){
						this.$message.error('请输入用户名');
						return
					}
					if(this.form.password==''){
						this.$message.error('请输入密码');
						return
					}
					if(this.form.phonenum==''){
						this.$message.error('请输入手机号');
						return
					}
					if(this.form.code==''){
						this.$message.error('请输入验证码');
						return
					}



					var obg={
						name:this.form.username,
						password:this.form.password,
						mobile:this.form.phonenum,
						sms_code:this.form.code,
						invite_code:this.form.yqcode,
						check_code:this.check_code
					}

					axios.post('/agent/user/register',obg).then((response)=>{
						if (response.data.msg.code == 0) {
							this.$message({
								message: '注册成功',
								type: 'success'
							});
							this.logintype = 1
						} else {

							this.$message.error(response.data.msg.info);
						}
					})


				}
				if(this.logintype==3){
					if(this.form.username==''){
						this.$message.error('请输入用户名');
						return
					}
					if(this.form.password==''){
						this.$message.error('请输入密码');
						return
					}
					if(this.form.phonenum==''){
						this.$message.error('请输入手机号');
						return
					}
					if(this.form.code==''){
						this.$message.error('请输入验证码');
						return
					}
					if(this.form.password!=this.form.passwordagin){
						this.$message.error('两次输入密码不同');
						return
					}
					//找回
					var obg={
						name:this.form.username,
						mobile:this.form.phonenum,
						sms_code:this.form.code,
						passwd:this.form.password,
						passwd_agin:this.form.passwordagin,
					}

					axios.post('/api/password/reset',obg).then((response)=>{
						if (response.data.msg.code == 0) {
							this.$message({
								message: '重置成功',
								type: 'success'
							});
							this.logintype = 1
						} else {
							this.$message.error(response.data.msg.info);
						}
					})



				}
			},
			//获取验证码
			getcode(){

				if(this.form.phonenum.length!=11){
					this.$message.error('请输入正确手机号');
					return
				}

				if(this.codemsg=='获取验证码'){
					this.codemsg = 60
					var time = setInterval(()=>{
						if(this.codemsg>0){
							this.codemsg--
						}else{
							this.codemsg = '获取验证码'
							clearInterval(time)
						}
					},1000)

          if(this.logintype==1){
            //手机号登录
            var obg = {
            	mobile:this.form.phonenum,
            }
            axios.post('/api/user/login/code/send',obg).then((response)=>{
            	if (response.data.msg.code == 0) {
            		this.$message({
            			message: '发送成功',
            			type: 'success'
            		});
            	} else {
            		this.$message.error(response.data.msg.info);
            	}
            })
          }

					if(this.logintype==2){
						//注册
						var obg = {
							mobile:this.form.phonenum,
							type:''
						}

						axios.post('/api/user/register/code/send',obg).then((response)=>{
							if (response.data.msg.code == 0) {
								this.$message({
									message: '发送成功',
									type: 'success'
								});
							} else {
								this.$message.error(response.data.msg.info);
							}
						})
					}

					if(this.logintype==3){
						if(this.form.username==''){
							this.$message.error('请输入用户名');
							return
						}
						//重置
						var obg = {
							mobile:this.form.phonenum,
							name:this.form.username
						}
						axios.post('/api/reset/code/send',obg).then((response)=>{
							if (response.data.msg.code == 0) {
								this.$message({
									message: '发送成功',
									type: 'success'
								});
							} else {
								this.$message.error(response.data.msg.info);
							}
						})
					}


				}

			},
			// 记住用户
			rememberUser() {
				const that = this;
				//判断复选框是否被勾选 勾选则调用配置cookie方法
				if (that.checked == true) {
					//传入账号名，密码，和保存天数三个参数
					that.setCookie(that.form.username, that.form.password, 7);
					console.log('that.form.username, that.form.password',that.form.username, that.form.password)
				} else {
					//清空Cookie
					that.clearCookie();
				}
			},

			//清除cookie
			clearCookie: function() {
				this.setCookie("", "", -1); //修改两个值都为空，天数为-1天就好了
			},
			//设置cookie
			setCookie(c_name, c_pwd, exdays) {
				let exdate = new Date(); //获取时间
				exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
				//字符串拼接cookie
				window.document.cookie =
					"account" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
				window.document.cookie =
					"password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
					console.log('window.document.cookie',window.document.cookie)
			},
			// 读取cookie
			getCookie: function() {
				let that = this;
				if (document.cookie.length > 0) {
					let arr = document.cookie.split("; "); //这里显示的格式需要切割一下自己可输出看下
					for (let i = 0; i < arr.length; i++) {
						let arr2 = arr[i].split("="); //再次切割
						//判断查找相对应的值
						if (arr2[0] == "account") {
							that.form.username = arr2[1]; //保存到保存数据的地方
						} else if (arr2[0] == "password") {
							that.form.password = arr2[1];
						}
					}
				}
			},
		}
	}
</script>

<style scoped>
	.t_body_bg{
		width:100vw;
		height: 100vh;
    background-size: cover;
    background-repeat: no-repeat;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}
	/deep/.t_del_com_input .el-input__inner{
		border: none !important;
		background-color: white !important;
	}
</style>
